package com.example.dongshihao.startproject_211dsh;

import android.content.res.Resources;
import android.os.AsyncTask;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dongshihao.startproject_211dsh.bean.WeatherInfo;
import com.example.dongshihao.startproject_211dsh.iface.WeatherListener;
import com.example.dongshihao.startproject_211dsh.model.Weather51Model;
import com.example.dongshihao.startproject_211dsh.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements WeatherListener, View.OnClickListener{
    private EditText etcity;
    private Button btnfind1,btnfind2;
    private TextView tvcityname,tvcityname2,temp,wet,wind,wd;
private RelativeLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        init();
    }

    private void init()
    {
        etcity=(EditText)findViewById(R.id.editText1);
        temp = (TextView)findViewById(R.id.temp);
        wd = (TextView)findViewById(R.id.wd);
        wet=(TextView)findViewById(R.id.wet);
        wind=(TextView)findViewById(R.id.wind);
        btnfind1=(Button)findViewById(R.id.button1);
        btnfind2=(Button)findViewById(R.id.button2);
        tvcityname=(TextView) findViewById(R.id.textView1);
        tvcityname2=(TextView) findViewById(R.id.textView2);
        btnfind1.setOnClickListener(this);
        btnfind2.setOnClickListener(this);

        layout=(RelativeLayout) findViewById(R.id.activity_main);
    }

    @Override
    public void onRespense(WeatherInfo weatherInfo) {
        System.out.println("---onRespense--activity");
        if(weatherInfo!=null)
        {
            tvcityname.setText("city:" + weatherInfo.getCity());
            temp.setText("温度：" + weatherInfo.getTemp());
            wet.setText("湿度：" + weatherInfo.getSD());
            wind.setText("风力：" + weatherInfo.getWS());
            wd.setText("风向：" + weatherInfo.getWD());

        }
        else
        {
            tvcityname.setText("未知");
            temp.setText("未知1");
            wet.setText("未知2");
            wind.setText("未知3");
            wd.setText("未知4");
        }
    }

    @Override
    public void onFail(String msg) {

        System.out.println("---onfail--activity");

        tvcityname.setText(msg+"here");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button1://中国天气网
                System.out.println("-------按钮1点击");
                String cityname=etcity.getText().toString();
                WeatherModel weatherModel=new WeatherModel();
                weatherModel.getWeather(cityname,this);

                if(cityname.equals("101070201"))
                    layout.setBackground(getResources().getDrawable(R.drawable.dl));
                if(cityname.equals("101010100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.b01));
                if(cityname.equals("101020100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.sh));
                if(cityname.equals("101030100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.tj));
                if(cityname.equals("101040100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.cq));


                break;
            case R.id.button2://51天气网
                System.out.println("------按钮2点击");
                String cityCode=etcity.getText().toString();
                Weather51Model weather51Model=new Weather51Model();
                weather51Model.getWeather(etcity.getText().toString(),this);


                if(cityCode.equals("101070201"))
                    layout.setBackground(getResources().getDrawable(R.drawable.dl));
                if(cityCode.equals("101010100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.b01));
                if(cityCode.equals("101020100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.sh));
                if(cityCode.equals("101030100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.tj));
                if(cityCode.equals("101040100"))
                    layout.setBackground(getResources().getDrawable(R.drawable.cq));


                break;
        }
    }
}
